import {Browser, Page} from "puppeteer";
import {CommonUtils} from "../../util/common-utils";

export class JdDorderOperation {

    operationOfDates(browser: Browser, page: Page): Promise<boolean> {
        return new Promise<boolean>(async (resolve, reject) => {

            while (true) {
                let pages = await browser.pages();
                page = pages[pages.length - 1];
                let orderDels = null;
                try {
                    orderDels = await page.$$("#order02 > div.mc > table > tbody");
                } catch (e) {
                    console.log("错误002", e);
                }
                if (orderDels != null && orderDels.length) {
                    let orderDelsLength = orderDels.length;
                    for (let i = 0; i < orderDelsLength; i++) {
                        let indexOrder = i;
                        let orderId = await page.evaluate(od => od.getAttribute("id"), orderDels[indexOrder]);
                        if (orderId.startsWith("parent-")) {
                            continue;
                        }
                        console.log("准备删除：", orderId)
                        try {
                            await CommonUtils.move(orderDels[indexOrder], page);
                            await CommonUtils.moveAndClic(await orderDels[indexOrder].$(".order-del"), page);
                        } catch (e) {
                            console.log("错误001", e);
                            i--;
                            break;
                        }
                        console.log("成功点击删除图标")
                        let sureDel = null;
                        let ii = 0;
                        while (sureDel == null && ii < 3) {
                            try {
                                console.log("尝试获取订单的确认删除按钮");
                                sureDel = await page.$("body > div.ui-dialog > div.ui-dialog-content > div > div > div.op-btns > a.btn-1.remove-order");
                            } catch (e) {
                                console.log("错误003", e);
                                ii++;
                                continue;
                            }
                            if (sureDel == null) {
                                ii++;
                            }
                        }
                        ii = 0;
                        console.log(sureDel)
                        while (sureDel == null && ii < 3) {
                            try {
                                console.log("尝试获取回收站的确认删除按钮");
                                sureDel = await page.$("body > div.ui-dialog > div.ui-dialog-content > div > div > div.op-btns > a.btn-1.delete-order");
                            } catch (e) {
                                console.log("错误003", e);
                                ii++;
                                continue;
                            }
                            if (sureDel == null) {
                                ii++;
                            }
                        }
                        if (sureDel != null) {
                            console.log("成功获取确认按钮")
                            await CommonUtils.moveAndClic(sureDel, page);
                            console.log("已删除：", orderId);
                        } else {
                            console.log("获取确认按钮失败，刷新页面");
                            await page.reload();
                            break;
                        }
                    }
                } else {
                    console.log("等待")
                    await CommonUtils.sleep(1000)
                }
                await CommonUtils.sleep(1000)
            }
            return resolve(false);
        });
    }
}